package com.wingsfrog.puzzle.service.impl;

import com.wingsfrog.puzzle.modules.dao.UserDao;
import com.wingsfrog.puzzle.modules.entity.User;
import com.wingsfrog.puzzle.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    UserDao userDao;

    @Override
    public Boolean notExist(String openid) {
        return userDao.existByOpenId(openid) == 0;
    }

    @Override
    public User get(Integer userId) {
        return userDao.findOne(userId);
    }

    @Override
    public User getByOpenId(String openId) {
        return userDao.findByOpenId(openId);
    }

    @Override
    public void saveUser(User user){
        userDao.save(user);
    }

    @Override
    public void saveUserInfo(String openId, String brif, Integer yearsold, Byte sex) throws Exception {
        User u = userDao.findByOpenId(openId);
        if (u == null) throw new Exception("not data");
        userDao.save(u);
    }

    @Override
    public void updateLoginTime(String openId) {
        User u = userDao.findByOpenId(openId);
        u.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
        userDao.save(u);
    }

    @Override
    public Integer getIdByOpenId(String openId) {
        return userDao.findByOpenId(openId).getId();
    }

    @Override
    public void updateCoins(int id, Integer coins) {
        userDao.updateCoins(id, coins);
    }
}
